<?php

namespace App\Entity\Trade;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Base\Annotation\Common;
use App\Entity\Lang\LangPack;
use App\Repository\Trade\CategoryTradeRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass=CategoryTradeRepository::class)
 */
class CategoryTrade
{
    /**
     * @Common(cnName="#",listType="text",editType="text")
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"cat"})
     */
    private $id;

    /**
     * @Common(cnName="名称",listType="text",editType="text",width="")
     * @ORM\Column(type="string", length=255)
     * @Groups({"cat"})
     */
    private $name;

    /**
     * @Common(cnName="链接",listType="text",editType="text",width="")
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Groups({"cat"})
     */
    private $url;

    /**
     * @Common(cnName="图标",listType="pic",editType="pic")
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Groups({"cat"})
     */
    private $icon;

    /**
     * @Common(cnName="排序",listType="text",editType="text")
     * @ORM\Column(type="integer", nullable=true)
     * @Groups({"cat"})
     */
    private $sort;

    /**
     * @Common(cnName="父栏目",listType="text",editType="select")
     * @ORM\ManyToOne(targetEntity="CategoryTrade", inversedBy="sons")
     * @Groups({"cat"})
     */
    private $parent;

    /**
     * @Common(cnName="子栏目",listType="text",editType="text")
     * @ORM\OneToMany(targetEntity="CategoryTrade", mappedBy="parent",orphanRemoval=true)
     * @ORM\OrderBy({"sort"="ASC"})
     * @Groups({"cat"})
     */
    private $sons;

    /**
     * @Common(cnName="类目折扣（%）",listType="text",editType="text")
     * @ORM\Column(type="float")
     * @Groups({"cat"})
     */
    private $cut;

    /**
     * @Common(cnName="语言包",listType="text",editType="text")
     * @ORM\ManyToOne(targetEntity=LangPack::class)
     * @Groups({"cat"})
     */
    private $lang;

    /**
     * @ORM\OneToMany(targetEntity=ProductTrade::class, mappedBy="cat")
     */
    private $products;

    public function __construct()
    {
        $this->sons = new ArrayCollection();
        $this->products = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getUrl(): ?string
    {
        return $this->url;
    }

    public function setUrl(?string $url): self
    {
        $this->url = $url;

        return $this;
    }

    public function getIcon(): ?string
    {
        return $this->icon;
    }

    public function setIcon(?string $icon): self
    {
        $this->icon = $icon;

        return $this;
    }

    public function getSort(): ?int
    {
        return $this->sort;
    }

    public function setSort(?int $sort): self
    {
        $this->sort = $sort;

        return $this;
    }

    public function getParent(): ?self
    {
        return $this->parent;
    }

    public function setParent(?self $parent): self
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * @return Collection|self[]
     */
    public function getSons(): Collection
    {
        return $this->sons;
    }

    public function addSon(self $son): self
    {
        if (!$this->sons->contains($son)) {
            $this->sons[] = $son;
            $son->setParent($this);
        }

        return $this;
    }

    public function removeSon(self $son): self
    {
        if ($this->sons->contains($son)) {
            $this->sons->removeElement($son);
            // set the owning side to null (unless already changed)
            if ($son->getParent() === $this) {
                $son->setParent(null);
            }
        }
        return $this;
    }

    public function getCut(): ?float
    {
        return $this->cut;
    }

    public function setCut(float $cut): self
    {
        $this->cut = $cut;

        return $this;
    }

    public function getLang(): ?LangPack
    {
        return $this->lang;
    }

    public function setLang(?LangPack $lang): self
    {
        $this->lang = $lang;

        return $this;
    }

    /**
     * @return Collection|ProductTrade[]
     */
    public function getProducts(): Collection
    {
        return $this->products;
    }

    public function addProducts(ProductTrade $products): self
    {
        if (!$this->products->contains($products)) {
            $this->products[] = $products;
            $products->setCat($this);
        }

        return $this;
    }

    public function removeProducts(ProductTrade $products): self
    {
        if ($this->products->contains($products)) {
            $this->products->removeElement($products);
            // set the owning side to null (unless already changed)
            if ($products->getCat() === $this) {
                $products->setCat(null);
            }
        }
        return $this;
    }

    public function addProduct(ProductTrade $product): self
    {
        if (!$this->products->contains($product)) {
            $this->products[] = $product;
            $product->setCat($this);
        }

        return $this;
    }

    public function removeProduct(ProductTrade $product): self
    {
        if ($this->products->contains($product)) {
            $this->products->removeElement($product);
            // set the owning side to null (unless already changed)
            if ($product->getCat() === $this) {
                $product->setCat(null);
            }
        }

        return $this;
    }
}
